#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <SDL/SDL.h>

#include "zombies.h"
#include "level.h"
#include "display.h"
#include "font.h"

#include "player_gfx.h"

// sprites surface
SDL_Surface *playersprites;

const char *getplayername(int code)
{
    if(code==1) return "Zeke";
    if(code==2) return "Julie";
    
    return "ERROR";
}

int playerclicked(int x, int y)
{
    if(x>level.player1_x-24 && x<(level.player1_x+24) && y>level.player1_y-24 && y<(level.player1_y+24))
    {
        return 1;
    }
    else
    if(x>level.player2_x-24 && x<(level.player2_x+24) && y>level.player2_y-24 && y<(level.player2_y+24))
    {
        return 2;
    }
    
    return -1;
}

void dragplayer(int id, int x, int y)
{
    if(id==1)
    {
        level.player1_x+=x;
        level.player1_y+=y;
    }
    else
    if(id==2)
    {
        level.player2_x+=x;
        level.player2_y+=y;
    }
}

void drawplayers()
{
    SDL_Rect src,dst;

    // player layer sizes
    src.w=dst.w=48;
    src.h=dst.h=48;
    src.y=0;
        
    // player 1 (Zeke)
    src.x=0;
    dst.x=level.player1_x-bgoffset.x-32;
    dst.y=level.player1_y-bgoffset.y-48;
    SDL_BlitSurface(playersprites,&src,screen,&dst);
    
    // player 2 (Julie)
    src.x=48;
    dst.x=level.player2_x-bgoffset.x-32;
    dst.y=level.player2_y-bgoffset.y-48;
    SDL_BlitSurface(playersprites,&src,screen,&dst);
}

void initplayers()
{
    SDL_Surface *temp;
    SDL_RWops *rw;

    // read bitmap binary stream
    rw=SDL_RWFromConstMem(player_bmp,sizeof(player_bmp));
    temp=SDL_LoadBMP_RW(rw,1); // automatically free RWops
    
    playersprites=SDL_ConvertSurface(temp,screen->format,SDL_SWSURFACE);
    SDL_FreeSurface(temp);

    // apply color mask
    SDL_SetColorKey(playersprites,SDL_SRCCOLORKEY|SDL_RLEACCEL,0xff00ff);
}